package com.ddatsh;

import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class StringUtils {
	private static final byte[] prk = { 48, -126, 4, -66, 2, 1, 0, 48, 13, 6,
			9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 4, -126, 4, -88, 48,
			-126, 4, -92, 2, 1, 0, 2, -126, 1, 1, 0, -84, 125, -47, 18, -105,
			-81, -50, 113, 102, 127, 12, -111, -63, 121, 13, 6, 52, -92, 112,
			-74, -93, 51, -1, 12, -87, 26, -54, 88, -10, 19, 35, 112, -80, -20,
			-41, 61, -68, 42, -84, -70, 66, -38, 126, -74, 61, -77, 90, 19, 12,
			-73, 27, 113, 65, -24, -80, -113, 79, 95, -68, -102, -41, 110, -2,
			-8, -72, -3, 57, 77, -3, -76, -72, 39, -36, 19, -127, -62, -122,
			92, 32, -52, -122, -10, -56, -128, 101, -83, 99, 118, 9, -62, -18,
			107, 3, 109, -20, 28, -118, 110, -5, 37, 123, 43, -118, 2, 58, 61,
			-99, 126, -75, -16, -48, -33, 85, -82, -59, 91, -78, -19, -99, -7,
			14, -79, -36, 5, 42, -32, -6, -72, -128, 76, -126, 88, -60, -23,
			-115, 12, 116, 22, -12, 33, -76, 107, -123, -109, -128, -62, 52,
			-103, 98, 98, 61, -78, -109, 91, 1, -56, 82, 9, -41, -19, -53, 1,
			64, 52, 6, -91, -110, -125, -97, -82, -49, 86, -113, -107, -75, 49,
			125, -56, -110, -26, -27, 97, -22, -15, 113, 38, 81, 74, -55, 87,
			-113, 49, -101, 105, 101, 69, -35, 32, -25, 20, -67, 100, 23, -53,
			44, 2, -33, -47, 41, 57, -103, 67, 57, -16, -7, 125, 59, 48, 11,
			114, 111, -65, -7, -123, -89, -12, 121, 54, 60, -52, -56, 25, 13,
			-7, -116, -123, 79, 109, -122, -116, -39, -99, 88, 13, -93, 98, 91,
			-23, -88, -11, -125, -46, 125, 122, -30, -31, 2, 3, 1, 0, 1, 2,
			-126, 1, 1, 0, -89, 115, -124, -15, -19, -69, -56, 72, 34, 94,
			-104, 79, 47, 105, 90, -111, 121, -83, 97, -106, -116, 29, 64, 46,
			-124, 67, -13, -56, 37, 43, 51, 46, -123, 51, -121, 72, -15, 80,
			-28, 89, -11, -24, -77, 54, 43, -13, -33, 47, -69, 26, 79, 83, -59,
			-50, 37, 9, 26, -33, 47, -13, -119, -52, -63, 77, -88, 24, 123, -9,
			16, -94, -50, 92, 51, -77, -28, 54, 121, -59, 26, 58, 51, -109,
			-125, -43, -47, -33, 95, 61, 10, 0, -24, -1, -114, 86, -71, -18,
			-125, 29, 118, 70, 78, -86, 87, -46, 13, -60, 31, 15, 17, -123,
			116, -32, 59, 7, -19, -92, 53, -79, -93, -72, 82, -31, -85, 92,
			-128, -31, -29, -62, 122, -40, 15, -84, 77, 93, -33, 14, -98, -67,
			-94, 81, 114, -72, -20, 48, -50, 96, -73, -102, 67, -117, 67, 88,
			-107, -42, -35, 112, 66, -39, -2, -61, -112, -59, 4, 41, 84, 44,
			48, 11, 94, -25, 105, 59, 95, 120, -99, -34, -35, 32, 81, 52, 40,
			-6, -105, -9, 41, 98, -80, -111, -92, -62, 71, 45, 71, 125, 95, 62,
			106, 72, 22, 57, 98, 122, -51, 62, 39, -34, 31, -49, 95, -11, -68,
			-17, 92, 63, 53, 17, -82, -30, -6, 12, 61, 97, -54, -95, 51, 75,
			-67, 73, 101, -87, 45, 36, 81, -55, -12, 117, 15, 126, 28, 84, 15,
			101, 84, -125, -48, -52, 65, 21, -24, -110, -126, 116, 94, -9, 54,
			121, 2, -127, -127, 0, -23, 114, -45, -51, -65, 103, 36, 49, 62,
			-57, 20, -31, 86, 14, -40, 11, 49, -49, 93, -10, 127, -84, 85, -53,
			-35, 121, 85, -10, -15, -118, -10, -87, -6, 5, -1, -36, -75, -44,
			-28, 31, -22, -71, -124, -29, 73, 43, -12, -8, 101, 26, -57, 87,
			100, 110, -59, 39, 89, -49, 94, 15, 125, 53, -71, -14, -56, 20, 91,
			107, -113, 32, -81, 80, 52, -118, 119, 6, 84, 122, 18, 89, -62, 70,
			94, -44, 61, -5, -33, 36, -67, 109, 84, 30, 9, 122, -20, 125, -115,
			27, 5, -114, -37, -112, 11, -109, 42, 77, 97, -48, -54, 68, -37,
			-2, 90, -17, 89, -3, -38, -110, -72, -58, 55, -90, 81, 3, -68, 124,
			118, 27, 2, -127, -127, 0, -67, 39, -122, 3, -92, 75, 103, 44, 32,
			-126, 19, -116, 72, -23, -14, -42, -64, -31, 32, 58, 28, 52, 59,
			127, -29, -12, -78, -52, 113, 80, 93, -28, 7, 72, -86, 50, 104,
			-118, -115, -36, -10, -105, 54, -14, -82, 68, 58, 86, -98, 104,
			114, -63, 15, 70, -128, 9, 16, -46, 94, 3, 82, 76, 0, -105, 88,
			125, -9, 28, 100, -106, -105, -117, 35, 2, 2, -6, 36, -15, 68, -42,
			99, 89, 47, -42, -84, 0, -64, 28, -8, -50, 0, -56, 91, -48, -36,
			80, -29, 21, -108, -96, -58, -20, 109, 71, -17, -118, -95, -69,
			106, -58, 58, -60, -122, -18, 74, -27, 22, 68, -82, 87, 7, 86, 122,
			51, -89, -7, -54, -77, 2, -127, -128, 88, 11, 75, -96, -124, 120,
			-108, -24, -47, -63, 105, -71, -106, -88, 96, -120, -48, 79, 98,
			35, -50, -127, 35, -106, -112, -65, -21, 81, 8, 70, 29, -61, 123,
			-99, 121, -93, 55, 28, -60, -80, -40, -23, 1, 26, 107, -123, 93,
			-115, -75, 44, 5, 84, -117, 119, 79, 122, 8, -32, 49, -100, 107,
			-33, -80, 114, -59, 77, 16, 49, -97, -72, 119, 6, 43, -38, -79, 11,
			61, 31, 16, -2, -77, 75, -79, 6, 33, 30, 118, 73, 55, -4, 28, -58,
			123, 82, -42, 16, -128, 0, 89, -9, 72, 13, -86, -97, -27, 75, 108,
			1, -65, -41, 122, -107, 18, -100, -60, -10, -3, -47, 106, 72, -81,
			-62, 85, 57, -80, -125, -52, 83, 2, -127, -128, 86, -95, 70, 88,
			94, -93, -122, 18, 8, -91, 106, 76, -126, -116, 91, 2, -115, -3,
			-51, 35, -80, 13, 31, -2, -9, -88, -9, 70, 120, -45, 36, -35, 45,
			-108, -45, 22, 25, 24, -13, -43, -14, 61, -39, 21, -106, 72, 123,
			43, 118, -117, 65, -101, 55, -30, -62, -6, -29, -16, 100, -52, 66,
			-95, -70, 87, -106, 88, -62, -105, 7, 94, -116, -13, 85, 74, 51,
			-112, -38, -32, -128, -11, 107, 52, -54, -101, 23, -43, -108, 38,
			-16, -80, 112, 93, 82, -25, -70, 47, 117, -127, 73, -69, -60, -65,
			61, 97, -121, -52, -57, 59, -83, -80, 49, -12, 22, 20, 6, -53, 26,
			-62, -61, -47, -23, -13, 106, 46, 68, 77, 59, -49, 2, -127, -127,
			0, -69, -106, -12, 20, -102, -83, -22, -92, -2, 10, 9, 100, -104,
			32, 51, 113, 36, 42, 77, -13, -64, -27, -89, -9, 64, 28, -65, -37,
			-17, -48, 57, -22, 116, 30, -57, -27, -38, 91, 117, -50, 111, -50,
			-103, -109, -95, -116, 45, -80, -22, -73, 8, -67, 22, 55, -110, 91,
			-113, 31, 75, -88, 58, 33, -73, 85, -114, 1, -42, 56, -32, -55, 2,
			-60, 5, -101, -56, 40, -29, -97, -35, 113, -74, 99, 41, -37, 32,
			44, -53, -25, -65, -4, 60, 88, 92, 28, 10, 116, 2, 104, -115, -56,
			73, 29, 60, 11, 3, -124, -64, 97, -11, -44, -96, -29, -123, 85, 30,
			-19, 96, 120, -74, 79, -83, -119, 64, 10, 125, -89, 115, -46 };

	private static final byte[] pk = { 48, -126, 1, 34, 48, 13, 6, 9, 42, -122,
			72, -122, -9, 13, 1, 1, 1, 5, 0, 3, -126, 1, 15, 0, 48, -126, 1,
			10, 2, -126, 1, 1, 0, -84, 125, -47, 18, -105, -81, -50, 113, 102,
			127, 12, -111, -63, 121, 13, 6, 52, -92, 112, -74, -93, 51, -1, 12,
			-87, 26, -54, 88, -10, 19, 35, 112, -80, -20, -41, 61, -68, 42,
			-84, -70, 66, -38, 126, -74, 61, -77, 90, 19, 12, -73, 27, 113, 65,
			-24, -80, -113, 79, 95, -68, -102, -41, 110, -2, -8, -72, -3, 57,
			77, -3, -76, -72, 39, -36, 19, -127, -62, -122, 92, 32, -52, -122,
			-10, -56, -128, 101, -83, 99, 118, 9, -62, -18, 107, 3, 109, -20,
			28, -118, 110, -5, 37, 123, 43, -118, 2, 58, 61, -99, 126, -75,
			-16, -48, -33, 85, -82, -59, 91, -78, -19, -99, -7, 14, -79, -36,
			5, 42, -32, -6, -72, -128, 76, -126, 88, -60, -23, -115, 12, 116,
			22, -12, 33, -76, 107, -123, -109, -128, -62, 52, -103, 98, 98, 61,
			-78, -109, 91, 1, -56, 82, 9, -41, -19, -53, 1, 64, 52, 6, -91,
			-110, -125, -97, -82, -49, 86, -113, -107, -75, 49, 125, -56, -110,
			-26, -27, 97, -22, -15, 113, 38, 81, 74, -55, 87, -113, 49, -101,
			105, 101, 69, -35, 32, -25, 20, -67, 100, 23, -53, 44, 2, -33, -47,
			41, 57, -103, 67, 57, -16, -7, 125, 59, 48, 11, 114, 111, -65, -7,
			-123, -89, -12, 121, 54, 60, -52, -56, 25, 13, -7, -116, -123, 79,
			109, -122, -116, -39, -99, 88, 13, -93, 98, 91, -23, -88, -11,
			-125, -46, 125, 122, -30, -31, 2, 3, 1, 0, 1 };

	public static final byte[] gs(byte[] data) {
		byte[] res = null;
		try {
			PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(prk);
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
			Signature sig = Signature.getInstance("SHA1withRSA");
			sig.initSign(priKey);
			sig.update(data);
			res = sig.sign();
		} catch (SignatureException ex) {
			ex.printStackTrace();
		} catch (InvalidKeyException ex) {
			ex.printStackTrace();
		} catch (InvalidKeySpecException ex) {
			ex.printStackTrace();
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return res;
	}

	public static final boolean vs(byte[] data, byte[] signature) {
		boolean flag = false;
		try {
			X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pk);
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
			Signature sig = Signature.getInstance("SHA1withRSA");
			sig.initVerify(pubKey);
			sig.update(data);
			flag = sig.verify(signature);
		} catch (SignatureException ex) {
			ex.printStackTrace();
		} catch (InvalidKeyException ex) {
			ex.printStackTrace();
		} catch (InvalidKeySpecException ex) {
			ex.printStackTrace();
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return flag;
	}

	public static final void generateKeyBytesArray() {
		KeyPairGenerator keygen;
		try {
			keygen = KeyPairGenerator.getInstance("RSA");
			keygen.initialize(2048);
			KeyPair keys = keygen.genKeyPair();
			System.out
					.println(byteArrayToString(keys.getPrivate().getEncoded()));
			System.out
					.println("################################################");
			System.out
					.println(byteArrayToString(keys.getPublic().getEncoded()));
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
	}

	public static final String byteArrayToString(byte[] ba) {
		String ret = "private static final byte[] a = { ";
		for (int i = 0; i < ba.length - 1; i++) {
			ret = ret + ba[i] + ", ";
		}
		ret = ret + ba[ba.length - 1] + " };";
		return ret;
	}

	public static void main(String[] args) {
		generateKeyBytesArray();
	}
}